<?php
	include_once('../sys/sys.php');
	include_once('jsss.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取已经完结的和未完结的总页面数
	$wjcontent = mb_convert_encoding(file_get_contents("http://www.89890.com/top/top5.htm"),"UTF-8","GB2312");
	$lzcontent = mb_convert_encoding(file_get_contents("http://www.89890.com/lz.htm"),"UTF-8","GB2312");
	
	
	$twjc = 1;
	$tlzc = 1;
	//开始整站采集
	//定义系统预置URL
	//完结的
	if($spc == 'wj'){
		getwj($wjcontent);
	}else if($spc == 'lz'){
		getlz($lzcontent);
	}else if($spc == 'all'){
		getlz($lzcontent);
		getwj($wjcontent);
	}
	
	echo "采集完毕";
	
	ob_end_flush();
	
	
	//以下为函数定义
	
	
	
	function getwj($content){
		$match_str = "/<a class=w5 href=\"..\/comic\/(.*)\" target=_blank>/isU";
		preg_match_all($match_str,$content,$urln);
		foreach ($urln[1] as $k => $v) {
			getsig($v);
		}
	}
	
	function getlz($content){
		//获取tids
		$match_str = "/<a class=w5 href=\"..\/comic\/(.*)\" target=_blank>/isU";
		preg_match_all($match_str,$content,$urln);
		foreach ($urln[1] as $k => $v) {
			getsig($v);
		}
	}
	
	function getsig($tid){
		global $DB;
		
		$url = 'http://www.89890.com/comic/' . $tid . '/';
		//分离URL以及ID
		$id = $tid;
		//定义公用变量
		if (substr($url,-1)!="/")
			$url.="/";
		$url_pre = $url;
		$pic_url_pre = 'http://www.89890.com/comic/';
		//获取URL内容
	//	$url = "1.htm";
		$content = 	file_get_contents($url);
		$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
		
		//获取名称
		//关键字 漫画_89890漫画网
		$name = explode('漫画_89890漫画网',$content);
		$name = $name[0];
		$name = explode('<title>',$name);
		$name = $name[1];
		
		//获取最后更新时间
		$dt = explode('更新：',$content);
		$dt = $dt[1];
		$dt = explode(' 备注：',$dt);
		$dt = strtotime(trim($dt[0]));
		
		//获取URL列表
		$urls = explode('<table width="100%" border="0" cellpadding="3" cellspacing="1"><tr>',$content);
		$urls = $urls[1];
		$urls = explode('</TD></table>',$urls);
		$urls = $urls[0];
		$replacer = array('<TD width="20%" height=22 vAlign=middle>',' </TD>','target=_blank ','·');
		$replacerc = array('');
		$urls = str_replace($replacer,$replacerc,$urls);
		
		
		$match_str = "/<a href = \'(.*)\'>/isU";
		preg_match_all($match_str,$urls,$urln);
		$urln = $urln[1];
		
		//由于是逆序，需要倒装数组(链接以及每个链接的数量)
		$urln = array_reverse($urln);
		
		echo '当前开始采集' . $name . ':http://www.89890.com/comic/' . $tid . "<br>";
		ob_flush();
		//插入分类信息
		//检查当前ID是否存在
		$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => '89890tags','where'	=> "1 " . $where ,)
		);	
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$dt,$name,0);
		}
		//获取数据库COUNT
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => '89890tags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		
		//构造采集相关内容
		//图片服务器数组
		$urlnc = array();
		//每集相关信息解析
		foreach ($urln as $k => $v) {
			//当COUNT小于已经采集的时候，跳过。
			if ($k < $dbcount) {
				continue;
			}
			$v = urlencode($v);
			//构造JS路径
			echo '处理当前JS链接:' . 'http://www.89890.com/comic/' . $tid . '/' . $v . '/index.js<br>';
			ob_flush();
			
			$contentc = get_js_url($tid, $v);
			$match_str = "/var total=(.*);var volpic='(.*)';var tpf = (.*);var tpf2 = (.*);/isU";
			preg_match_all($match_str,$contentc,$contentcn);
			$urlnc[$k]['total'] = $contentcn[1][0];
			$urlnc[$k]['volpic'] = mb_convert_encoding($contentcn[2][0],"UTF-8","GB2312");
			$urlnc[$k]['tpf2'] = $contentcn[4][0];
			//获取该集算法编号
			$urlnc[$k]['vol'] = get_pic_cal(file_get_contents('http://www.89890.com/comic/' . $tid . '/' . $v . '/'));
			//入库
			//当COUNT小于已经采集的时候，跳过。
			
			do_photoinsert($tid,$urlnc[$k]['volpic'],$urlnc[$k]['total'],$urlnc[$k]['tpf2'],$urlnc[$k]['vol']);
		}
		//更新计数
		$where = 'id= ' . $id .'';
		$DB->do_update('89890tags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urln),
			 ),
			 $where
		);
	}
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '89890tags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count,$tpf2,$vol){
		global $DB;
		$DB->do_insert( '89890photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								   'tpf2'					=> $tpf2,
								   'vol'					=> $vol,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
?>